import QtQuick 2.2
import C2048 1.0

Item {
    id: root
	width: 500; height: 685;

    C2048{
        id: c2048;
        rows: 4;
        columns: 4;
    }

	Rectangle{
		id: container
		anchors.fill: parent
		
		Info2048 {
			id: info;
			anchors.bottom: grid.top;
            //anchors.horizontalCenter: grid.horizontalCenter;
            scoreNum: c2048.score;
            bestNum: c2048.bestScore;
			onNewGame: {
				c2048.init();
                root.show();
                root.focus = true;
			}
		}
		
        GridPanel{
			id: grid;
			anchors.top: info.bottom;
			//anchors.horizontalCenter: info.horizontalCenter;
		}
	}
	
		

    function clear() {
         for(var i = 0; i < 16; i++) {
            grid.cell.itemAt(i).tileText = "";
			grid.cell.itemAt(i).color = "#CCC0B3";
        }
    }

    function show() {
        clear();
        for(var i = 0; i < 4; i++) {
            for(var j = 0; j < 4; j++){
                if(c2048.show(i,j)) {
                   grid.cell.itemAt(i*4+j).tileText = c2048.show(i,j);
				   grid.cell.itemAt(i*4+j).color = c2048.color(i,j);
                }
            }
        }
        info.scoreNum = c2048.score;
        info.bestNum = c2048.bestScore;
    }

    Keys.onPressed: {
        switch(event.key) {
            case Qt.Key_Up:
                c2048.moveKey(C2048.Move_Up);
                root.show();
                break;
            case Qt.Key_Down:
                c2048.moveKey(C2048.Move_Down);
                root.show();
                break;
            case Qt.Key_Left:
                c2048.moveKey(C2048.Move_Left);
                root.show();
                break;
            case Qt.Key_Right:
                c2048.moveKey(C2048.Move_Right);
                root.show();
                break;
            default:
                break;
            }
    }
}
